package flyshoes.mybatis.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

@Data
public class PageQuery {
    private Integer pageNum = 1;//
    private Integer pageSize = 5;//页面大小
    private String sortBy;//排序字段
    private boolean isAsc = true;//是否升序


    public <T> Page<T> toMpPage(OrderItem ... orders) {
        Page<T> page = Page.of(pageNum, pageSize);//分页条件
        if (StrUtil.isNotBlank(sortBy)) {
            //排序条件
            OrderItem orderItem = new OrderItem();
            //不为空
            orderItem.setColumn(sortBy);
            orderItem.setAsc(isAsc);
            page.addOrder(orderItem);
        } else if (orders != null) {
            //为空，默认排序
            page.addOrder(orders);
        }
        return page;
    }

    public <T> Page<T> toMpPage(String defaultSortBy, boolean isAsc) {
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn(defaultSortBy);
        orderItem.setAsc(isAsc);
        return this.toMpPage(orderItem);
    }

    public <T> Page<T> toMpPageDefaultSortByCreateTimeDesc() {
        return toMpPage("created_time", false);
    }

    public <T> Page<T> toMpPageDefaultSortByUpdateTimeDesc() {
        return toMpPage("updated_time", false);
    }
    public <T> Page<T> toMpPageDefaultSortByPrice() {
        return toMpPage("price", true);
    }
}
